package org.i1510.base.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.i1510.base.po.BasePo;
import org.i1510.base.service.IBasePoService;
import org.i1510.base.utils.BeanUtils;
import org.i1510.base.utils.DateUtils;
import org.i1510.base.utils.IDGenerator;
import org.i1510.base.utils.web.ResultMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/admin/basePo")
public class BasePoController extends BaseController {


	@Value("${app.name}")
    private String appName;

	@Autowired
	private IBasePoService iBasePoService;


	/**
	 * @param docId
	 * @return
	 * @throws Exception
	 * http://127.0.0.1:8099/admin/basePo/findId/1234
	 */
	@RequestMapping(value = "/findId/{docId}", method = RequestMethod.GET)
	@ResponseBody
	public String  findId(@PathVariable("docId") String docId)
			throws Exception {
		ResultMapper mapper = null;
		mapper = ResultMapper.generateFailure();
		mapper.appendResult("messages", iBasePoService.findId(docId));
		mapper.appendResult("appName", appName);
		return mapper.toJson();
	
	}
	/**
	 * @param basePo
	 * @return
	 * @throws Exception
	 * http://127.0.0.1:8099/admin/basePo/add.do?docId=1234&docStatus=20
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	@ResponseBody
	public String  add(@ModelAttribute("basePo") BasePo basePo)
			throws Exception {
		ResultMapper mapper = null;
		mapper = ResultMapper.generateFailure();
		
		basePo.setDocId(IDGenerator.getGenerateID());
		basePo.setDocStatus("10");
		basePo.setDocCreateTime(DateUtils.getNow());
		if(iBasePoService.add(basePo) > 0){
			mapper = ResultMapper.generateSuccess();
			mapper.appendResult("docId", basePo.getDocId());
		}else{
			mapper.appendResult("messages", "提交失败!");
		}
		return mapper.toJson();
	}
	/**
	 * @param docId
	 * @return
	 * @throws Exception
	 * http://127.0.0.1:8099/admin/basePo/del/1234
	 */
	@RequestMapping(value = "/del/{docId}", method = RequestMethod.GET)
	@ResponseBody
	public String  del(@PathVariable("docId") String docId)
			throws Exception {
		ResultMapper mapper = null;
		mapper = ResultMapper.generateFailure();
		mapper.appendResult("messages", iBasePoService.del(docId));
		mapper.appendResult("appName", appName);
		return mapper.toJson();
	}
	/**
	 * @param basePo
	 * @return
	 * @throws Exception
	 * http://127.0.0.1:8099/admin/basePo/update.do?docId=1234&docStatus=20
	 */
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	@ResponseBody
	public String  update(@ModelAttribute("basePo") BasePo basePo)
			throws Exception {
		ResultMapper mapper = null;
		mapper = ResultMapper.generateFailure();
		if(null != basePo && !basePo.getDocId().equals("")){
			List list = iBasePoService.findId(basePo.getDocId());
			if(list.size() == 1){
				Map finder = (Map)list.get(0);
				BasePo tans = new BasePo();
				BeanUtils.mapCopyObject(finder, tans);
				BeanUtils.copy(tans, basePo);
				tans.setDocLastTime(DateUtils.getNow());
				//tans.setDocLastor(UserUtils.getSessionUserBean(request).getSysUser().getDocId());			
				if(iBasePoService.update(tans) > 0){
					mapper = ResultMapper.generateSuccess();
					mapper.appendResult("docId", basePo.getDocId());
				}else{
					mapper.appendResult("messages", "提交失败!");
				}
			}else{
				mapper.appendResult("messages", "多个ID无效!");
			}
		}else{
			mapper.appendResult("messages", "Id无效!");
		}
		
		
		return mapper.toJson();
	}
	@RequestMapping(value = "/adds", method = RequestMethod.GET)
	@ResponseBody
	public String  adds()
			throws Exception {
		ResultMapper mapper = null;
		mapper = ResultMapper.generateFailure();
		BasePo basePo = new BasePo();
		basePo.setDocId(IDGenerator.getGenerateID());
		basePo.setDocStatus("12");
		basePo.setDocCreateTime(DateUtils.getNow());

		BasePo basePo1 = new BasePo();
		basePo1.setDocId(IDGenerator.getGenerateID());
		basePo1.setDocStatus("11");
		basePo1.setDocCreateTime(DateUtils.getNow());
		
		List list = new ArrayList();
		list.add(basePo);
		list.add(basePo1);
		
		if(iBasePoService.adds(list) > 0){
			mapper = ResultMapper.generateSuccess();
			mapper.appendResult("docId", basePo.getDocId());
		}else{
			mapper.appendResult("messages", "提交失败!");
		}
		return mapper.toJson();
	}

}
